﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition.Hosting;

namespace NetFrameworkExtensions.Enterprise.Composition
{
  internal class MEFServiceManagerAdapter :
    ServiceManagerBase
  {
    CompositionContainer compositionContainer;

    public MEFServiceManagerAdapter(CompositionContainer compositionContainer)
    {
      if (compositionContainer == null)
        throw new ArgumentException("compositionContainer");
      this.compositionContainer = compositionContainer;
    }

    protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
    {
      var lazyResults = compositionContainer.GetExports(serviceType, null, null);
      return lazyResults.Select(l => l.Value);
    }

    protected override object DoGetInstance(Type serviceType, string key)
    {
      return compositionContainer.GetExports(serviceType, null, key).Single().Value;
    }
  }
}
